package com.controller;

import com.entity.Smbms_bill;
import com.entity.Smbms_provider;
import com.entity.Smbms_role;
import com.entity.Smbms_user;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.service.ISmbms_UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class IndexController {
    @Autowired
    private ISmbms_UserService sus;

    @RequestMapping(value = "/Long",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public  Smbms_user Long(@RequestParam Map<String,String> mp)throws Exception{
        String name = mp.get("username");
        System.out.println("------------name"+name);
        Smbms_user su =sus.selectL(mp);
        return su;
    }

    @RequestMapping(value = "/info",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public  Map<String,Object> info(@RequestParam Map<String,String> pm)throws Exception{
        Map<String,Object> rm =new HashMap<>();
        List<Smbms_bill> list =sus.selectAll(pm);
        System.out.println(pm.get("pid"));
        System.out.println(pm.get("isP"));
        System.out.println(pm.get("pname"));
        rm.put("list",list);
        return rm;
    }

    @RequestMapping(value = "/providerInfo",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Map<String,Object> providerInfo(@RequestParam Map<String,String> mp)throws Exception{
        Map<String,Object> rm = new HashMap<>();
        List<Smbms_provider> list = sus.selectAllprovider(mp);
        rm.put("list",list);
        return rm;
    }

    @RequestMapping(value = "/insert",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int insert(@RequestBody Smbms_bill sb)throws Exception{
       int num =sus.insert(sb);
        return num;
    }

    @RequestMapping(value = "/delete",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int delete(@RequestParam Map<String,String> mp)throws Exception{
       String id =mp.get("id");
        System.out.println(id);
        int num=0;
       if(id!=null) {
            num = sus.deleteByPrimaryKey(Long.parseLong(id));
       }
        return num;
    }

    @RequestMapping(value = "/selectD",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Smbms_bill selectD(@RequestParam Map<String,String> mp )throws Exception{
        String id =mp.get("id");
        Smbms_bill sb=null;
        if(id!=null) {
            sb = sus.selectByPrimaryKey(Long.parseLong(id));
        }
        return sb;
    }

    @RequestMapping(value = "/update",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int update(@RequestBody Smbms_bill sb)throws Exception{
        int num = sus.updateByPrimaryKeySelective(sb);
        return num;
    }

    @RequestMapping(value = "/pDinfo",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Smbms_provider pDinfo(@RequestParam Map<String,String> mp)throws Exception{
        Smbms_provider list =sus.selectByPrimaryKeySP(Long.parseLong(mp.get("id")));
        return list;
    }

    @RequestMapping(value = "/pinsert",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int pinsert(@RequestBody Smbms_provider sp)throws Exception{
        sp.setCreationdate(new Date());
        int num = sus.insertSelectiveSP(sp);
        return num;
    }

    @RequestMapping(value = "/pdelete",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int pdelete(@RequestParam Map<String,String> mp)throws Exception{
        int num =sus.deleteByPrimaryKeySP(Long.parseLong(mp.get("id")));
        return num;
    }

    @RequestMapping(value = "/pupdate",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int pupdate(@RequestBody Smbms_provider sp)throws Exception{
        int num = sus.updateByPrimaryKeySelectiveSP(sp);
        return num;
    }

    @RequestMapping(value = "/srAll",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public List<Smbms_role> srAll()throws Exception{
        List<Smbms_role> list =sus.selectAll();
        return list;
    }

    @RequestMapping(value = "/suAll",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Map<String,Object> suAll(@RequestParam Map<String,String> mp)throws Exception{
        Map<String,Object> map =new HashMap<>();
        int pageno ; //当前页数
        int pagesize=5; //每页显示几条数据
        try {
            pageno=Integer.parseInt(mp.get("pno"));
        }catch (Exception e){
            pageno=1;
        }
        Page<Object> page = PageHelper.startPage(pageno,pagesize);
        List<Smbms_user> list =sus.selectAllSR(mp);
        map.put("pagenum", page.getPageNum());//当前页数
        map.put("pagecount", page.getPages());//总页数
        map.put("list",list);//总数据
        map.put("Total",page.getTotal());
        return map;
    }

    @RequestMapping(value = "/suSelectD",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Smbms_user suSelectD(@RequestParam Map<String,String> mp)throws Exception{
        String id =mp.get("id");
        Smbms_user su=null;
        if(id!=null) {
            su = sus.selectByPrimaryKeySR(Long.parseLong(id));
        }
        return su;
    }

    @RequestMapping(value = "/suDelete",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int suDelete(@RequestParam Map<String,String> mp)throws Exception{
        String id =mp.get("id");
        int num =0;
        if(id!=null) {
             num = sus.deleteByPrimaryKeySR(Long.parseLong(id));
        }
        return num;
    }

    @RequestMapping(value = "/suInsert",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int suInsert(@RequestBody Smbms_user su)throws Exception{
        su.setCreationdate(new Date());
        int num =sus.insertSelectiveSR(su);
        return num;
    }
    @RequestMapping(value = "/suUpdate",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public int suUpdate(@RequestBody Smbms_user su)throws Exception{
        int num =sus.insertSelectiveSR(su);
        return num;
    }
}
